package com.zhou.combine.security.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.zhou.combine.security.entity.Users;
import com.zhou.combine.security.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CustomUserService
 *
 * @author zhouxianfeng
 * @date 2021-1-6
 */
@Service
public class CustomUserService implements UserDetailsService {

    @Autowired
    private IUsersService usersService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<Users> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Users::getUsername,username);
        Users one = usersService.getOne(queryWrapper);
        if (one == null) {
            throw new UsernameNotFoundException("can not found username " + username);
        }
        return new UserPrincipal(one,getRoleCodes(one));
    }

    private List<String> getRoleCodes(Users user){
        return Lists.newArrayList();
    }

}
